const conn=require("../db/db.js")

const showIndexPage=(req,res)=>{
    let pagesize=3;
    let nowpage=req.query.page || 1;//你传的是第几页 就应该获取哪个数字

    let offset=(nowpage-1)*pagesize
    //查询福哦呦文章数据 循环每一篇文章显示一个a列表
    let sql=`select a.id,a.title,a.ctime,u.nickname from blog_articles a left join blog_users u on a.authorId=u.id order by a.ctime desc limit ${offset},${pagesize};
    select count(*) as count from blog_articles`
    conn.query(sql,(err,result)=>{
        if(err) return console.log("出错了");
        console.log(result);
        // let totalpage=result[1][0].count;
       let totalpage=Math.ceil(result[1][0].count/pagesize)
        
        res.render("index.ejs",{
            user:req.session.user,
            islogin:req.session.islogin,
            articles:result[0],
            totalpage:totalpage,
            nowpage:nowpage
        })
    })
}

module.exports = {
    showIndexPage
}